﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MiscUtil.IO;

using ATM_Pro.Module;
using System.Net;
using System.Threading;

namespace ATM_Pro
{
    public class LivePacketHandle : IPacketHandler
    {
        public LivePacketHandle()
        {
            lpb = new LivePacketBean();
        }

        public event ImageDataChangeHandle DataChange;

        private LivePacketBean lpb;
        public LivePacketBean LivePack
        {
            get { return lpb; }
        }

        #region IPacketHandler Members

        public bool CanHandle(byte[] bytes)
        {
            return (IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 8)) == 4);
        }

        public void Handle(byte[] bytes)
        {
            var memStream = new MemoryStream(bytes);
            var reader = new EndianBinaryReader(new MiscUtil.Conversion.BigEndianBitConverter(), memStream);

            //skip aa55aa55
            reader.Seek(4, SeekOrigin.Begin);

            var length = reader.ReadInt32();
            if (length + 8 == bytes.Length)
            {
                var packType = reader.ReadInt32();
                var imgTime = GetLocalTime(reader.ReadInt32());
                var atmId = reader.ReadByte();
                var camId = reader.ReadByte();
                var addId = reader.ReadInt16();
                var imgType = reader.ReadByte();
                var imgLength = reader.ReadInt32();
                var imgData = reader.ReadBytes(imgLength);

                ATM tempAtm = new ATM(atmId, Enum.GetName(typeof(AtmAddress), addId), addId, new Camera(camId, PacketType.RealtimeMessage));
                lpb.AtmBean = tempAtm;
                lpb.ImgType = imgType;
                lpb.LiveTime = imgTime;
                lpb.LiveImg = imgData;//没有把图片的字节截出来
                lpb.LiveImgLength = imgLength;
                lpb.PackLength = length;

                if (imgLength < 0) return;
                var imgStream = new MemoryStream(imgData);
                System.Drawing.Image img = System.Drawing.Image.FromStream(imgStream);
                string savePath = ImageHandle.GenerateSavePath(lpb.AtmBean, lpb.LiveTime, count++, 2);
                img.Save(savePath);
                img.Dispose();


                if (preMunite != imgTime.Minute)
                {
                    count = 0;
                    CompressionVideoParams cvp = new CompressionVideoParams();
                    cvp.AtmBean = tempAtm;
                    cvp.PacketType = Enum.GetName(typeof(PacketType), packType);
                    cvp.Path = savePath;
                    cvp.PreTime = imgTime;

                    VideoHandle vh = new VideoHandle(cvp);
                    Thread th = new Thread(vh.HasWarnInfoOrTransInfo);
                    th.Start();
                }
                preMunite = imgTime.Minute;
                OnDataChanged(this, new DataChangeEventArgs(this.GetType().Name));
            }
        }
        int second = -1;
        int count = 0;
        int preMunite = DateTime.Now.Minute;
        #endregion

        protected void OnDataChanged(object sender, DataChangeEventArgs e)
        {
            if (this.DataChange != null)
            {
                DataChange(sender, e);
            }
        }

        protected DateTime GetLocalTime(int second)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(second).ToLocalTime();
        }

    }
}
